<template>
	<u-upload :limitType="['png', 'jpg', 'jpeg',  'gif']" :fileList="fileList" @afterRead="afterRead" uploadIcon='plus'
		@delete="deletePic" :name="name" :multiple="multiple" :maxCount="maxCount" :uploadText="uploadText"
		:previewImage='previewImage' :width='width' :height='height' :disabled="disabled">
	</u-upload>
</template>

<script>
	import {
		HTTP_REQUEST_URL,
		HEADER,
		STATIC_URL
	} from '@/config/app';
	export default {
		name: 'uploadImage',
		props: {
			disabled: {
				type: Boolean,
				default: false
			},
			width: {
				type: String,
				default: '200rpx'
			},
			height: {
				type: String,
				default: '200rpx'
			},
			multiple: {
				type: Boolean,
				default: false
			},
			previewImage: {
				type: Boolean,
				default: true
			},
			show: {
				type: Boolean,
				default: false
			},
			maxCount: {
				type: Number,
				default: 1
			},
			uploadText: {
				type: String,
				default: ''
			},
			name: {
				type: String,
				default: 'my-upload'
			},
		},

		data() {
			return {
				fileList: [],
				images: []
			}
		},
		onLoad(option) {},
		computed: {
			head() {
				let token = this.$store.state.app.token;
				let head = {};
				head["Authori-zation"] = token;
				//head["Content-Type"] = "multipart/form-data"
				return head;
			},
			action() {
				return HTTP_REQUEST_URL + '/shop/upload/image'
			}
		},
		methods: {
			clear() {
				this.fileList = []
				this.images = []
			},
			add(img) {
				this.fileList.push({
					url: STATIC_URL + img
				})
				this.images.push(img)
			},
			deletePic(event) {
				this.fileList.splice(event.index, 1)
				this.images.splice(event.index, 1)
				this.$emit("delete", this.images)
			},
			// 新增图片
			async afterRead(event, fields, name) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileList.length
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					let result = await this.uploadFilePromise(lists[i].url)
					console.log(result);
					result = JSON.parse(result)
					if (result.code == 200) {
						let item = this.fileList[fileListLen]

						this.fileList.splice(fileListLen, 1, Object.assign(item, {
							status: 'success',
							message: '',
							url: STATIC_URL + result.data.url
						}))
						this.images.push(result.data.url)
						this.$emit("success", this.images, result)
						fileListLen++
						console.log(this.images);
					} else {
						this.fileList.splice(fileListLen, 1)
						uni.showToast({
							title: result.message,
							icon: 'none',
							duration: 3000
						})
					}


				}
			},
			uploadFilePromise(url) {
				let a = this.action
				let head = this.head
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: a, // 仅为示例，非真实的接口地址
						filePath: url,
						header: head,
						name: 'file',
						success: (res) => {
							let filename = url.substr(url.lastIndexOf('/') + 1)
							let filePath = url

							resolve(res.data)
						},
						fail: (err) => {
							console.log("失败》》", err)
						}
					});
				})
			},


		}
	}
</script>

<style>

</style>